
$(document).ready(function(){
		
	$('#birthsday').datepicker({ 
		dateFormat: 'yy-mm-dd',
		changeMonth: true,
		changeYear: true,
		rangeSelect: true,
		yearRange: "1950:now",
		maxDate: '-1y'
	 });
	
	$('#password').blur(function(){
		//alert("потеря фокуса елементом password");
	});
	
	$("#registerForm").validate({
		rules:{
			name:{
				required: true
			},
			surname:{
				required: true
			},
			country:{
				required: true
			},
			birthsday:{
				required: true
			},
			email:{
				required: true
			},
			password:{
				required: true
			},
			password2:{
				required: true
			}
		},
//		messages:{
//			password:{
//				required: "Enter your password",
//				minLength: "At least 2 characters are necessary"
//			},
//			required: "Required"
//		}
//		,
		errorPlacement: function(error, element) {
			 error.appendTo(element.parent().next());
			}
		
	});
	
	//checking email availability
	$(function(){
	$("#sub").click(function(){
		// get user data
		var checkString = $("#email").val();
		// form request string
		var data = 'email='+ checkString;
		//if checkString is not emty:
		if(checkString){
			//do ajax request
			$.ajax({
				type: "POST",
				url: "register/do_check_email.htm",
				data: data,
				dataType: "text",
				//before sending
				beforeSend: function(html){
					//alert("beforeSend");
				},
//				error:function(result){
//					alert("error: " + error);
//				},
				//gets results
				success: function(responce){
					$("#emailResults").html(responce);
				},
				error:  function(xhr, str){
				    alert('Возникла ошибка: ' + xhr.responseCode);
				}
			});
		}
		return false;
	});
	});
	
	$(function(){
	$("#country").change(function(){
		var countryNum = $("#country :selected").val();
		if(countryNum == 0){
			$("#countryError").text('This field is required.');
		} else{
			$("#countryError").text('');
		}
	});
	});
	
	$(function(){
	$("#registerForm").submit(function(eventObject){
		var countryNum = $("#country :selected").val();
		if(countryNum == 0){
			$("#countryError").text('This field is required.');
			eventObject.preventDefault;
		}
		if($("#password2") != $("#password")){
			$("#password2Error").text("Passwords are different!");
			eventObject.preventDefault;
		}
	});
	});
	
	$(function(){
	$("#email").change(function(){
		if($(this).val() == ""){
			$("#emailResults").text('');
		}
	});
	});

	
	$(function(){
	$("#password2").change(function(){
		var errorLabel = $("#password2Error");
		if($(this).val() == ""){
			errorLabel.text("This field is required.");
		}
		if($(this).val() != $("#password").val()){
			errorLabel.text("Passwords are different!");
		}else{
			errorLabel.text("");
		}
		
	});
	});
		
	//focus functions
	$(function() {$(":input:text, :input:password")
		.focus(function() {$(this).addClass('onFocus');} )
		.blur(function() {$(this).removeClass('onFocus');} );
	});
});

















